<!--
 * @Author: 谢小康 2170673706@qq.com
 * @Date: 2023-11-23 19:55:16
 * @LastEditors: 谢小康 2170673706@qq.com
 * @LastEditTime: 2024-02-04 09:54:51
 * @FilePath: \my_bolg_admin\my_admin\src\components\login.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div class="body">
        <div class="login-container">
            <h2>欢迎登录</h2>
            <el-form :model="form" :rules="rules" label-width="80px" class="demo-ruleForm" ref="ruleFormRef" show-message
                status-icon>
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="form.name" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" show-password />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                    <el-button @click="Registrat()">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import {
    Userlogin
} from '../api/users';
import { Userstore } from '../store/User'
import { usePremission } from '../store/premission.js'
import { useRouter, useRoute } from 'vue-router';
import { dealWithRoute } from "../util/premission.js"
let router = useRouter()
let route = useRoute()
let Premissionstore = usePremission()
const userstore = Userstore()
const form = reactive({
    name: '',
    password: '',
})
const rules = reactive({
    name: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],

})
const ruleFormRef = ref(null)
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            let res = await Userlogin(form.name, form.password)
            if (res.data.status == 200) {
                ElNotification({
                    title: 'Success',
                    message: '登录成功',
                    type: 'success',
                })
                /* 登录后存token到localstorage 获取用户信息*/
                userstore.storageUserInfo(res.data.data);
                userstore.islogin = true
                await Premissionstore.getUserRouters()
                let routers = JSON.parse(localStorage.getItem("routerlist"))
                dealWithRoute(routers)
                Premissionstore.createmenu(routers)
                /* 跳转首页*/
                router.replace('/home');
            } else {
                ElNotification({
                    title: '错误',
                    message: res.data.message,
                    type: 'success',
                })
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
const Registrat = () => {
    router.push('/register')
}
onMounted(() => {
    if (route.query) {
        let { name, password } = route.query
        form.name = name
        form.password = password
    }else{
        form.name = ''
        form.password = ''
    }
})
</script>

<style scoped>
.body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(to right, #7F7FD5, #86A8E7, #91EAE4);
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.login-container {
    background-color: #fff;
    padding: 40px;
    padding-left: 10px;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    text-align: center;
    animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.login-container h2 {
    margin-bottom: 30px;
    color: #333;
    font-size: 24px;
}

.login-container input[type="text"],
.login-container input[type="password"] {
    width: 100%;
    padding: 12px;
    margin-bottom: 20px;
    border-radius: 5px;
    border: 1px solid #ccc;
    box-sizing: border-box;
    transition: border-color 0.3s ease;
}

.login-container input[type="text"]:focus,
.login-container input[type="password"]:focus {
    outline: none;
    border-color: #7F7FD5;
}

.login-container input[type="submit"] {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 5px;
    background-color: #7F7FD5;
    color: #fff;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.login-container input[type="submit"]:hover {
    background-color: #6B6BAA;
}
</style>